﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class MP_PositionTypes : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        private IEmployeeTypeBLL empTypeBLL = new EmployeeTypeBLL();
        private IPositionTypeBLL postypeBLL = new PositionTypeBLL();
        private IClassBLL classBLL = new ClassBLL();
        private ITreeViewBLL treeBLL = new TreeViewBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                bindEmpType();
                //LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
                //RadTreeNode selected_node = RadTreeView1.SelectedNode;
                
                //LoadDataGrid();
                //lblHeader.Text = "กำหนดประเภทตำแหน่ง";
            }
        }
        private void bindEmpType()
        { 
            IList<EmployeeTypeInfo> infoList = empTypeBLL.ListEmployeeTypes();
            ddlEmployeeType.DataSource = infoList;
            ddlEmployeeType.DataBind();
             
        }
        public void LoadDataGrid()
        {
            Nullable<int> EmpType = string.IsNullOrEmpty(ddlEmployeeType.SelectedValue) ? (int?)null : Int32.Parse(ddlEmployeeType.SelectedValue);
            string HeaderText = EmpType.HasValue ? EmpType == 1 ? "ชื่อประเภทตำแหน่ง" : "ชื่อกลุ่มงาน" : "ชื่อประเภทตำแหน่ง";
            
            IList<PositionTypeInfo> Info = postypeBLL.ListPositionTypes(null, EmpType);
            RadGrid1.DataSource = Info;
            RadGrid1.Columns[3].HeaderText = HeaderText;
            RadGrid1.MasterTableView.EditFormSettings.UserControlName = "../UserControls/MP_PositionTypes.ascx";
            RadGrid1.DataBind();
        }
        public void LoadDataGridClass(string PosTypeSeq)
        {
            string EmpType = ddlEmployeeType.SelectedValue;
            string HeaderText = string.IsNullOrEmpty(EmpType) ? "ชื่อระดับตำแหน่ง" : EmpType == "1" ? "ชื่อระดับตำแหน่ง" : "ชื่อระดับชั้นงาน";
            
            IList<ClassInfo> Info = classBLL.ListClasses(PosTypeSeq);
            RadGrid1.DataSource = Info;
            RadGrid1.Columns[3].HeaderText = HeaderText;
            RadGrid1.MasterTableView.EditFormSettings.UserControlName = "../UserControls/MP_Classes.ascx";
            RadGrid1.DataBind();
        }

        private void LoadRootNodes(RadTreeView treeView, TreeNodeExpandMode expandMode)
        {
            IList<TreeViewInfo> infoList = treeBLL.LoadPositionTypeTreeView("", ddlEmployeeType.SelectedValue);

            foreach (TreeViewInfo info in treeBLL.LoadPositionTypeTreeView("", ddlEmployeeType.SelectedValue))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id.Trim();
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                treeView.Nodes.Add(node);
            }
        }

        private void PopulateNodeOnDemand(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        {
            e.Node.Dispose();
            e.Node.Nodes.Clear();

            var ds = treeBLL.LoadPositionTypeTreeView(e.Node.Value.Trim(), ddlEmployeeType.SelectedValue);
            foreach (TreeViewInfo info in ds)
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id.Trim();
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                e.Node.Nodes.Add(node);
            }

            e.Node.Expanded = true;
            e.Node.ExpandMode = expandMode;
        }

        public void PopulateNodeOnEditGrid(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        {
            foreach (TreeViewInfo info in treeBLL.LoadPositionTypeTreeView(e.Node.Value.Trim(), ddlEmployeeType.SelectedValue))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id.Trim();
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                e.Node.Nodes.Add(node);
            }

            e.Node.Nodes.Clear();
            e.Node.ExpandMode = expandMode;
        }
        public void RefreshTree()
        {
            RadTreeNode selected_node = RadTreeView1.SelectedNode;
            if (selected_node == null)
            {
                selected_node = RadTreeView1.FindNodeByValue("0");
            }
            RadTreeNodeEventArgs node = new RadTreeNodeEventArgs(selected_node);
            RadAjaxManager1.ResponseScripts.Add(@"CollapseSelectTree();");
            PopulateNodeOnEditGrid(node, TreeNodeExpandMode.ServerSideCallBack);
            RadAjaxManager1.ResponseScripts.Add(@"ExpandSelectTree();");
        }
        protected void btnExpand_Click(object sender, ImageClickEventArgs e)
        {
            IList<TreeViewInfo> infoList = treeBLL.LoadPositionTypeTreeView("", ddlEmployeeType.SelectedValue);

            RadTreeView1.Nodes.Clear();
            RadTreeView1.DataSource = infoList;
            RadTreeView1.DataBind();
            RadTreeView1.ExpandAllNodes();
        }
        protected void btnCollapse_Click(object sender, EventArgs e)
        {
            RadTreeView1.CollapseAllNodes();
        }
        protected void cmdExpand_Click(object sender, ImageClickEventArgs e)
        {
            IList<TreeViewInfo> infoList = treeBLL.LoadPositionTypeTreeView("", ddlEmployeeType.SelectedValue);

            RadTreeView1.Nodes.Clear();
            RadTreeView1.DataSource = infoList;
            RadTreeView1.DataBind();
            RadTreeView1.ExpandAllNodes();
        }

        protected void cmdCollapse_Click(object sender, ImageClickEventArgs e)
        {
            RadTreeView1.CollapseAllNodes();
        }

        protected void RadTreeView1_NodeExpand(object sender, RadTreeNodeEventArgs e)
        {
            PopulateNodeOnDemand(e, TreeNodeExpandMode.ServerSideCallBack);

        }
        protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            Session["EMPTYPE"] = ddlEmployeeType.SelectedValue;

            if (RadTreeView1.SelectedNode.Level == 0)
            {
                if (e.CommandName == "Delete" && RadTreeView1.SelectedNode.Level == 0)
                {
                    int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"]);

                    postypeBLL.Delete(seq, ssMng.EmpSeqI);
                }
                LoadDataGrid();
            }
            else if (RadTreeView1.SelectedNode.Level == 1)
            {
                if (e.CommandName == "Delete" && RadTreeView1.SelectedNode.Level == 1)
                {
                    int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"]);
                    classBLL.Delete(seq, ssMng.EmpSeqI);
                }
                LoadDataGridClass(lblPosTypeSeq.Text.Trim());
            }
            RefreshTree();
        }

        protected void RadTreeView1_NodeClick(object sender, RadTreeNodeEventArgs e)
        {
            RadTreeNode selected_node = RadTreeView1.SelectedNode;

            string EmpType = ddlEmployeeType.SelectedValue;
            string EmpTypeName = string.IsNullOrEmpty(ddlEmployeeType.SelectedValue) ? "" : ddlEmployeeType.SelectedItem.Text;
            string value = selected_node.Value.Trim();
            string name = selected_node.Text.Trim();
            string[] root_value = selected_node.Value.Split(new string[] { "-" }, StringSplitOptions.None);

            if (!string.IsNullOrEmpty(value))
            {
                if (EmpType == "1")// ข้าราชการ
                {
                    if (selected_node.Level == 0)
                    {
                        LoadDataGrid();
                        lblHeader.Text = "ข้อมูลประเภทตำแหน่ง " + EmpTypeName;
                    }
                    else if (selected_node.Level == 1)
                    {
                        lblPosTypeSeq.Text = root_value[1].Trim();
                        Session["POSTYPE"] = lblPosTypeSeq.Text;
                        LoadDataGridClass(lblPosTypeSeq.Text.Trim());
                        lblHeader.Text = "ข้อมูลระดับตำแหน่งประเภท" + name;
                    }
                }
                else if (EmpType == "2") // ลูกจ้างประจำ
                {
                    if (selected_node.Level == 0)
                    {
                        LoadDataGrid();
                        lblHeader.Text = "ข้อมูลกลุ่มงาน " + EmpTypeName;
                    }
                    else if (selected_node.Level == 1)
                    {
                        lblPosTypeSeq.Text = root_value[1];
                        Session["POSTYPE"] = lblPosTypeSeq.Text;
                        LoadDataGridClass(lblPosTypeSeq.Text.Trim());
                        lblHeader.Text = "ข้อมูลระดับชั้นงานของ" + name;
                    }
                }
                else
                {
                    if (selected_node.Level == 0)
                    {
                        LoadDataGrid();
                        lblHeader.Text = "ข้อมูลกลุ่มงาน " + EmpTypeName;
                    }
                    else if (selected_node.Level == 1)
                    {
                        lblPosTypeSeq.Text = root_value[1];
                        Session["POSTYPE"] = lblPosTypeSeq.Text;
                        LoadDataGridClass(lblPosTypeSeq.Text.Trim());
                        lblHeader.Text = "ข้อมูลระดับชั้นงานของ" + name;
                    }
                }
            }
        }
        protected void cmdSearch_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void ddlEmployeeType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            Session["EMPTYPE"] = ddlEmployeeType.SelectedValue;
            string EmpTypeName = string.IsNullOrEmpty(ddlEmployeeType.SelectedValue) ? "" : ddlEmployeeType.SelectedItem.Text;

            RadTreeView1.Nodes.Clear();

            LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
            RadTreeNode selected_node = RadTreeView1.SelectedNode;
            if (RadTreeView1.Nodes.Count > 0)
            {
                RadTreeView1.Nodes[0].Selected = true;
                //Session["TreeNodeValue"] = RadTreeView1.Nodes[0].Value;
            }

            LoadDataGrid();
            lblHeader.Text = ddlEmployeeType.SelectedValue == "1" ? "กำหนดประเภทตำแหน่ง" : "กำหนดข้อมูลกลุ่มงาน";
            lblHeader.Text = lblHeader.Text + " " + EmpTypeName;
        }
    }
}
